// spitslug.txt
// By Lazarus
// Thanks a bunch to Niemand for all the help with the targeting algorithm.
// Slug uses ranged attack, does 75-175 damage, and charms, confuses, dumbfounds and enfeebles target.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1 - How much to increase flag by.
//   Cell 2 - If add half an exp, then set this to 5.
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i,status,type,next,new,targ,best,mx,my,i,px,py;

body;
beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
if(char_ok(4) == 1)
{
	if(get_memory_cell(2) == 5)
		inc_flag(250,5,(get_ran(1,get_memory_cell(1),get_memory_cell(1) + 1)));
	if(Get_memory_cell(2) != 5)
		inc_flag(250,5,get_memory_cell(1));
}	
break;

beginstate START_STATE; 
	// Look for a target, attack it if visible
	next = 3;
	
	set_state_continue(4);
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		set_state_continue(3);
	}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((target_ok() == FALSE) || (can_see_char(get_target()) == 0))
		set_state(START_STATE);
	if(new == 1){ //Mix things up and choose a new target
		next = 3;
		set_state_continue(4);
	}
	px = char_loc_x(get_target);
	py = char_loc_y(get_target);
	mx = my_loc_x();
	my = my_loc_y();
	set_character_pose(ME,1);
	force_instant_terrain_redraw();
	put_straight_zap((mx),(my),(px),(py),2);
	put_effect_on_char(get_target(),13,1,0);
	run_animation();
	damage_char(get_target,get_ran(5,15,35),2);
	set_char_status(get_target(),8,12,0,0); //charmed
	set_char_status(get_target(),13,25,0,0); //dumbfounded
	set_char_status(get_target(),18,15,0,0); //confused
	set_char_status(get_target(),21,10,0,0); //Enfeebled
	print_str("The poisons cloud your mind.");
	new = 1;
	if(get_flag(6,8) == 0) {
		message_dialog("The slug breathes a stream of what you thought was acid. But when it hits you your mind grows foggy, it must be some kind of halucinogenic.","");
					if(get_flag(250,0) == 1)
						message_dialog("Glup shouts a warning to you, _Watch out, this is the type of slug that poisoned our chieftain. That poison is very potent.","");
					set_flag(6,8,1); }
	set_character_pose(Me,2);
	force_instant_terrain_redraw();
	end_combat_turn();
break;

beginstate 4; //Targetting state
	targ = -1;
	best = 30000;
	i = 0;
	while(i < 120){
		if((char_ok(i) == 1) && (char_attitude_to_char(ME,i) == 2) && (dist_to_char(i) < 12) && can_see_char(i) && (get_char_status(i,29) == 0)){
			status = get_ran(1,0,100);
			if(i < 6) //a party member
				status = get_ran(1,0,50);
			if(status < best){
				targ = i;
				best = status;
			}
		}
		i = i + 1;
	}
	set_target(ME,targ);
	if(targ == -1)
		set_state(START_STATE);
	new = 0;
	set_state_continue(next);
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;